package org.darcy.service.work.impl;

import java.util.List;

import org.darcy.dao.WorkTaskMapper;
import org.darcy.entity.privilege.SysUser;
import org.darcy.entity.work.WorkInstance;
import org.darcy.entity.work.WorkTask;
import org.darcy.framework.constant.SysConstant;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.service.privilege.SysUserService;
import org.darcy.service.work.WorkService;
import org.darcy.service.work.WorkTaskService;
import org.darcy.utils.UserTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * WorkTask服务实现
 */
@Service
@Transactional
public class WorkTaskServiceImpl extends GenericServiceImpl<WorkTask> implements WorkTaskService {

	@Autowired
	private WorkTaskMapper mapper;

	@Autowired
	@Lazy
	private WorkService WorkService;

	@Autowired
	@Lazy
	private SysUserService SysUserService;

	@Override
	public WorkTask getDetail(Integer id) {
		WorkTask record = mapper.selectById(id);
		return record;
	}

	/**
	 * 处理工作任务
	 */
	@Override
	public void processWork(WorkInstance param) {
		// 存草稿：同步生成我的工作任务
		WorkTask draft = this.getDraftTask(param.getId());
		if (draft.getId() == null) {
			this.insert(draft);
		}
		// 提交：同步生成审批者的工作任务
		if (param.getTask().getOperator() != null) {
			WorkTask approveTask = this.getApprovingTask(draft);
			if (approveTask.getId() == null) {
				approveTask.setOperator(param.getTask().getOperator());
				approveTask.setComment(param.getTask().getComment());
				this.insert(approveTask);
			}
		}
	}

	@Override
	public WorkTask getDraftTask(Integer work_id) {
		WorkTask draft = null;
		QueryWrapper<WorkTask> wrapper = new QueryWrapper<WorkTask>();
		wrapper.eq("work_id", work_id).orderByAsc("id");
		List<WorkTask> list = super.getList(wrapper);
		if (list != null && !list.isEmpty()) {
			WorkTask firstOne = list.get(0);
			if (firstOne.getStatus().equals(SysConstant.TaskStatus.STATUS0_TODO)) {
				return firstOne;
			}
			for (WorkTask a : list) {
				if (a.getStatus().equals(SysConstant.TaskStatus.STATUS0_TODO)
						&& a.getOperator().equals(firstOne.getOperator())) {
					draft = a;
				}
			}
		}
		return draft;
	}

	@Override
	public WorkTask getApprovingTask(WorkTask draftTask) {
		// 先查找，不存在则新建
		WorkTask approveTask = this.getBusinessTask(draftTask.getWorkId(), draftTask.getId());
		if (approveTask != null) {
			return approveTask;
		}
		approveTask = draftTask.clone();
		approveTask.setId(null);
		approveTask.setComment(null);
		approveTask.setWorkId(draftTask.getWorkId());
		approveTask.setPid(draftTask.getId());
		return approveTask;
	}

	@Override
	public WorkTask getCCTask(Integer workId, Integer uid) {
		QueryWrapper<WorkTask> wrapper = new QueryWrapper<WorkTask>();
		wrapper.eq("work_id", workId).eq("operator", uid).eq("status", SysConstant.FlowStatus.STATUS9_CC);
		return this.getOne(wrapper);
	}

	@Override
	public List<WorkTask> getTaskList(Integer work_id) {
		QueryWrapper<WorkTask> wrapper = new QueryWrapper<WorkTask>();
		wrapper.eq("work_id", work_id);
		wrapper.orderByDesc("id");
		List<WorkTask> list = super.getList(wrapper);
		return list;
	}

	@Override
	public WorkTask getBusinessTask(Integer work_id, Integer pid) {
		QueryWrapper<WorkTask> wrapper = new QueryWrapper<WorkTask>();
		wrapper.eq("work_id", work_id).eq("pid", pid);
		return this.getOne(wrapper);
	}

	@Override
	public WorkTask transfer(WorkTask param) {
		// 查找工作任务，必须是草稿状态
		WorkTask task = this.getById(param.getTaskId());

		WorkTask transferTask = new WorkTask();
		transferTask.setPid(task.getId());
		transferTask.setWorkId(task.getWorkId());
		transferTask.setOperator(param.getTransferTo());
		String name = SysUserService.getById(param.getTransferTo()).getRealName();
		transferTask.setOperatorName(name);
		transferTask.setComment(task.getComment() + "->转交给" + name);
		this.insert(transferTask);
		task.setStatus(SysConstant.TaskStatus.STATUS1_DONE);// 老任务从草稿变为完成
		this.update(task);
		// 更新工作项
		WorkInstance work = WorkService.getById(task.getWorkId());
		work.setOperatorName(name);
		work.setTitle(transferTask.getComment());
		WorkService.update(work);
		return transferTask;
	}

	@Override
	public WorkTask getApprovedTask(String formType, Integer formId) {
		WorkTask approvedTask = null;
		// 查找工作项
		WorkInstance param = WorkInstance.builder().formType(formType).formId(formId).build();
		WorkInstance work = WorkService.getBusinessWork(param);
		// 查找审批通过的任务
		QueryWrapper<WorkTask> wrapper = new QueryWrapper<WorkTask>();
		wrapper.eq("work_id", work.getId()).eq("status", SysConstant.TaskStatus.STATUS1_DONE);
		List<WorkTask> list = super.getList(wrapper);
		if (!list.isEmpty()) {
			approvedTask = list.get(0);
		}
		return approvedTask;
	}

}
